package com.twh.www.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝配置类
 * 用于配置支付宝客户端所需的参数
 */
@Configuration
public class AlipayConfig {
    /**
     * 支付宝网关
     * 沙箱环境使用：https://openapi.alipaydev.com/gateway.do
     * 正式环境使用：https://openapi.alipay.com/gateway.do
     */
    private static final String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 应用ID
     * 在支付宝开放平台创建应用后获取
     */
    private static final String APP_ID = "2021000148683457";

    /**
     * 商户私钥
     * 使用支付宝开放平台提供的工具生成
     * 注意：需要妥善保管，不要泄露
     */
    private static final String PRIVATE_KEY ="MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWDYP8NFnFkAO9d8bVnVJBKe8eiCuPWIuLkpAOfcCg8QzL5td0S5eZAajrxYS1gDR9hzjE2hI+dYQNC0PWS7mp/OHlwXmEX/TK81gL8Zhah+YTJVpUg0anvcd8KQ+gcAtO2owBJli5L4Z7r3lWpNhHirTvSatEsOhLcaDZcD8RbCL5bvPn4fihUYxnNxbTq3VL55XZHmJqK7aERPdT4HzaS82QbDFI1Qp1JMab/klPnVZn9P+9sognq1uG2jX2ZAwFCSfGVKz8SOSFInfM1+B1q4wBI98HsSg+71fqLv8jXkoIMKbsjJCU6Beo7/G/MKwAisJ6TfPo1CaKoJ96+DwLAgMBAAECggEAStQOn7TTl8wLsW6Lv3cmz8xDrKwnXOLBJCdAo5leeGP9B6nkg+A2h1zmij0DwydeBcNrMe+17WPZKFAX0kQNIJI1bMgg+zyG7LaRj9M5MNxYhe+4rjCRCZGAa5BmdJGrcfVWoDQpRqBGpmOJVQX7vSGUvUe1mxnFnenaVtS8/shyYGtb2QrIaJp7d7w70LDrLAcDgIfYkh8enque1Stlh+Ca66TJ6fx9ZFojd7OxN++fTrUFMD0r5qInjMEdumJ+Ug2nCc1dXhScRufQiRVFNHjG1iHTAHrj+L07Bx2yY0pd5v7Zc/1FMDzFuKiXYMhY+xxzAi1qBY0t1FuibVluoQKBgQDmGbmfj5r4QSqXWwJXMbradgmZ3ZyjdTLzThF8ASD/mZxZoEEkeHnzEHfGJAADNsqYL5fWu0ZYomFHgLz5ffgPt7mTLilkR8ouZaYMhXCU+RyVkBioB5ppo3wd5v21tgaDBYvybcNo/9I8x9/n0L/d2QVlDephxp74uxGfcCfNswKBgQCm8T2jZJYCjtiMOVer/uWXteAeW4ON+B9/wUx1GPw4qAyg8ejNMA6VO4pNhBQS5yuzgID5R9YaZ33ygcb9jybQartm2ocQB4aKq0IlPo28vSpoQ7NtwEQUKaxg4Jyahdq8nO7dbJt+jIAqY58h/90PXGoK06QZFvM2unnoTOUcSQKBgGDtn0L2xGolUWK2xLKkUTjrljnJn80/dnoFfdoFw7fIJpOerTJGyeA5JKtDyPmRoCAhlE2xT/3QUThEo9ZZWSCN3U+Bl5AVqbaHvT3zDCzFD2R1cd1+bm5nnsYd4uwjxHR25q/1yuY1NYPDEq/dQKWcYOhjx0Lu31KZwt+t6CZ/AoGACB5FJBP5I4i0ySTkuY18tsL2ciN7I0VEqt7IjA05/rtVWeJJsui6XNF3aUuZ+k1Zl7IRE45FwCT2P/vFpID4nzwBfv/zf3kONuIbJlqDZAFC9a0NJbIq3VwlcNSbaPxHZdKv3v6wFRhaBzWvAGVD58JTudEO8tVl4GxzDzPmFFkCgYEA0TOeWbrxgyTjQVyFthhaF5vnINQSBOKxbdSgTs2/bOVSHNuD2CreXZehlSVjnQXjIcgN5JQ2cg6esjicbeuABZ2pd+TrIqRyqM807mbnkK4t0DXogZMkZ/7C9E/vXT7r9eU94dYB/oLkCpwnBzxOWeFsHZYqHpcszcJNFBMyrqk=";
    /**
     * 支付宝公钥
     * 在支付宝开放平台获取
     * 用于验证支付宝的异步通知
     */
    private static final String ALIPAY_PUBLIC_KEY ="MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlg2D/DRZxZADvXfG1Z1SQSnvHogrj1iLi5KQDn3AoPEMy+bXdEuXmQGo68WEtYA0fYc4xNoSPnWEDQtD1ku5qfzh5cF5hF/0yvNYC/GYWofmEyVaVINGp73HfCkPoHALTtqMASZYuS+Ge695VqTYR4q070mrRLDoS3Gg2XA/EWwi+W7z5+H4oVGMZzcW06t1S+eV2R5iaiu2hET3U+B82kvNkGwxSNUKdSTGm/5JT51WZ/T/vbKIJ6tbhto19mQMBQknxlSs/EjkhSJ3zNfgdauMASPfB7EoPu9X6i7/I15KCDCm7IyQlOgXqO/xvzCsAIrCek3z6NQmiqCfevg8CwIDAQAB";
    /**
     * 异步通知地址
     * 支付宝服务器主动通知商户服务器里指定的页面http/https路径
     * 建议使用https协议,必须是外网地址，不然支付宝服务器无法返回支付是否成功
     */
    private static final String NOTIFY_URL = "http://localhost:8080/alipay/notify";

    /**
     * 同步返回地址
     * 支付完成后，用户从支付宝页面跳转回商户页面的地址
     */
    private static final String RETURN_URL = "http://localhost:8080/alipay/return";
    /**
     * 创建支付宝客户端
     * @return AlipayClient实例
     */
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
                URL,                // 支付宝网关
                APP_ID,            // 应用ID
                PRIVATE_KEY,       // 商户私钥
                "json",           // 请求格式
                "UTF-8",          // 编码格式
                ALIPAY_PUBLIC_KEY, // 支付宝公钥
                "RSA2"            // 签名方式
        );
    }
}
